package com.kedacom.demo.healthcheck;

import java.io.IOException;

import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

/**
 * RabbitMQ健康检查
 * @author luocanfeng
 * @date 2016年9月2日
 */
public class RabbitMQHealthCheckItem extends HealthCheckItem {

	private String rabbitmqHost;
	private int rabbitmqManagementPort = 6674;
	private String rabbitmqUsername = "dev";
	private String rabbitmqPassword = "dev";

	private static String rabbitmq_aliveness_url = "http://%s:%d/api/aliveness-test/%%2f";

	{
		type = Type.RabbitMQ;
	}

	public RabbitMQHealthCheckItem() {
		type = Type.RabbitMQ;
	}

	public RabbitMQHealthCheckItem(String name) {
		this();
		this.name = name;
	}

	public RabbitMQHealthCheckItem(String name, String rabbitmqHost) {
		this(name);
		this.rabbitmqHost = rabbitmqHost;
	}

	public RabbitMQHealthCheckItem(String name, String rabbitmqHost, int rabbitmqManagementPort) {
		this(name, rabbitmqHost);
		this.rabbitmqManagementPort = rabbitmqManagementPort;
	}

	public RabbitMQHealthCheckItem(String name, String rabbitmqHost, int rabbitmqManagementPort,
			String rabbitmqUsername, String rabbitmqPassword) {
		this(name, rabbitmqHost, rabbitmqManagementPort);
		this.rabbitmqUsername = rabbitmqUsername;
		this.rabbitmqPassword = rabbitmqPassword;
	}

	/**
	 * 检查RabbitMQ是否正常
	 */
	@Override
	public boolean doCheck() {
		CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
		credentialsProvider.setCredentials(new AuthScope(rabbitmqHost, rabbitmqManagementPort),
				new UsernamePasswordCredentials(rabbitmqUsername, rabbitmqPassword));
		CloseableHttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(credentialsProvider)
				.build();

		try {
			HttpGet request = new HttpGet(String.format(rabbitmq_aliveness_url, rabbitmqHost, rabbitmqManagementPort));
			CloseableHttpResponse response = httpClient.execute(request);
			try {
				return response.getStatusLine().getStatusCode() == 200;
			} finally {
				response.close();
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				httpClient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return false;
	}

	public static void main(String[] args) {
		RabbitMQHealthCheckItem healthCheck = new RabbitMQHealthCheckItem("RabbitMQ健康检查", "172.16.185.233");
		System.out.println(healthCheck.doCheck());
	}

	public String getRabbitmqHost() {
		return rabbitmqHost;
	}

	public void setRabbitmqHost(String rabbitmqHost) {
		this.rabbitmqHost = rabbitmqHost;
	}

	public int getRabbitmqManagementPort() {
		return rabbitmqManagementPort;
	}

	public void setRabbitmqManagementPort(int rabbitmqManagementPort) {
		this.rabbitmqManagementPort = rabbitmqManagementPort;
	}

	public String getRabbitmqUsername() {
		return rabbitmqUsername;
	}

	public void setRabbitmqUsername(String rabbitmqUsername) {
		this.rabbitmqUsername = rabbitmqUsername;
	}

	public String getRabbitmqPassword() {
		return rabbitmqPassword;
	}

	public void setRabbitmqPassword(String rabbitmqPassword) {
		this.rabbitmqPassword = rabbitmqPassword;
	}

}
